package util;

import java.io.File;
import java.util.Calendar;
import java.util.Date;

/**
 * Utility class for test cases common functions.
 *
 * @author pradeeph
 *
 */
public class TestCaseUtil
{
    private static String[] pdfUploadFiles = null;

    static
    {
        loadPDFUploadFiles();
    }

    /**
     * load PDF upload files
     */
    private static void loadPDFUploadFiles()
    {
        String uploadFolder = FileUtil.getDocUploadDirectoryPath();
        File[] files = FileUtil.listFilesInDirectory(uploadFolder,"pdf");

        if (files != null && files.length > 0)
        {
            pdfUploadFiles = new String[files.length];
            int index = 0;
            for(File file: files)
            {
                pdfUploadFiles[index++] = file.getName();
            }
        }
    }

    /**
     * get the list of PDF upload documents name
     *
     * @return
     */
    public static String[] getUploadPDFFilesList()
    {
        return pdfUploadFiles;
    }

    /**
     * get the random PDF upload file
     *
     * @return
     */
    public static String getRandomUploadPDFFile()
    {
        return pdfUploadFiles[CommonUtil.nextIntInRange(0, pdfUploadFiles.length - 1)];
    }

    /**
     * get from and to year combination<br>
     * examples:
     * <ul>
     * <li>2001</li>
     * <li>2010-2011</li>
     * <li>2011-2013</li>
     * </ul>
     *
     * @return
     */
    public static String getFromAndToYear()
    {
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        int startYear = CommonUtil.nextIntInRange(2000, currentYear - 1);
        int endYear = CommonUtil.nextIntInRange(startYear, currentYear);

        return (startYear == endYear ? startYear : startYear + "-" + endYear).toString();
    }

    /**
     * get random start and end page numbers<br>
     * examples:
     * <ul>
     * <li>27</li>
     * <li>1-10</li>
     * <li>25-28</li>
     * </ul>
     *
     * @return
     */
    public static String getRandomPages()
    {
        int startPage = CommonUtil.nextIntInRange(1, 100);
        int endPage = CommonUtil.nextIntInRange(startPage, 200);

        return (startPage == endPage ? startPage : startPage + "-" + endPage).toString();
    }

    /**
     * get the volume of publication or publication events
     *
     * @return
     */
    public static String getVolume()
    {
        return "v" + CommonUtil.nextIntInRange(1, 100);
    }

    /**
     * get the issue of publication or publication events
     *
     * @return
     */
    public static String getIssue()
    {
        return "" + CommonUtil.nextIntInRange(1, 200);
    }

    /**
     * get random number
     *
     * @return
     */
    public static long getRandomNumber()
    {
        return Calendar.getInstance().getTimeInMillis();
    }

    /**
     * get random month as a number <br>
     * Here the first month starts with number <b>1</b>.
     *
     * @return
     */
    public static final int getRandomMonth()
    {
        return CommonUtil.nextIntInRange(1, 12);
    }

    /**
     * get a random month's abbreviations<br>
     * examples:
     * <ul>
     * <li>Jan</li>
     * <li>Feb</li>
     * </ul>
     *
     * @return string
     */
    public static final String getRandomAbbrMonthString()
    {

        String[] QuarterTypes = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };

        return QuarterTypes[CommonUtil.nextIntInRange(0, QuarterTypes.length - 1)];
    }

    /**
     * get a random month's full name<br>
     * examples:
     * <ul>
     * <li>January</li>
     * <li>February</li>
     * </ul>
     *
     * @return string
     */
    public static final String getRandomMonthFullString()
    {

        String[] QuarterTypes = new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September",
                "October", "November", "December" };

        return QuarterTypes[CommonUtil.nextIntInRange(0, QuarterTypes.length - 1)];
    }

    /**
     * get random year between currentYear-10 to currentYear+1
     *
     * @return
     */
    public static int getRandomYear()
    {
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);

        return CommonUtil.nextIntInRange(currentYear - 10, currentYear + 1);
    }

    /**
     * get random year between currentYear to currentYear+2
     *
     * @return
     */
    public static int getAboveRandomYear()
    {
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);

        return CommonUtil.nextIntInRange(currentYear, currentYear + 2);
    }

    /**
     * get current year
     *
     * @return
     */
    public static int getCurrentYear()
    {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * get random date
     *
     * @return
     */
    public static Date getRandomDate()
    {
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        calendar.set(Calendar.YEAR, TestCaseUtil.getRandomYear());
        calendar.set(Calendar.MONTH, TestCaseUtil.getRandomMonth() - 1);

        // to get the randomize date
        calendar.set(Calendar.DATE, CommonUtil.nextIntInRange(1, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)));

        return calendar.getTime();
    }

    /**
     * get random date in selected year
     *
     * @return
     */
    public static Date getRandomDateInSelectedYear(int year)
    {
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, TestCaseUtil.getRandomMonth() - 1);

        // to get the randomize date
        calendar.set(Calendar.DATE, CommonUtil.nextIntInRange(1, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)));

        return calendar.getTime();
    }

    /**
     * pick a random quarter
     *
     * @return
     */
    public static String getRandomQuarter()
    {
        String[] QuarterTypes = new String[] { "Fall", "Winter", "Spring", "Summer" };

        return QuarterTypes[CommonUtil.nextIntInRange(0, QuarterTypes.length - 1)];
    }

    /**
     * attach documents for web testing with document name all documents must be
     * save in "/resource/doc-upload" location
     *
     * @param docName
     *
     * @return
     */
    public static String attachDocument(String docName)
    {
        return FileUtil.getDocUploadDirectoryPath() + File.separatorChar + docName;
    }

    public static void main(String[] args)
    {
        System.out.println(getRandomDate());
        System.out.println(getRandomDate());
        System.out.println(getRandomDate());
        System.out.println(getRandomDate());
    }

    /**
     * get numeric day between 1 and 28
     *
     * @return
     */
    public static int getRandomDay()
    {
        return CommonUtil.nextIntInRange(1, 28);
    }

    /**
     * get the academic year start date based on date-argument<br>
     * like <b>July 01, YYYY</b>
     *
     * @param date
     * @return
     */
    public static Calendar getAcademicYear(Date date)
    {
        Calendar currentAcademicYear = getAcademicYear(Calendar.getInstance().get(Calendar.YEAR));
        Calendar nextAcademicYear = currentAcademicYear;

        if (currentAcademicYear.getTime().before(date))
        {
            nextAcademicYear.add(Calendar.YEAR, 1);
        }

        return nextAcademicYear;
    }

    /**
     * get the academic year start date based on today's date<br>
     * like <b>July 01, YYYY</b>
     *
     * @return
     */
    public static Calendar getAcademicYear()
    {
        return getAcademicYear(new Date());
    }

    /**
     * get the academic year start date based on year-argument<br>
     * like <b>July 01, YYYY</b>
     *
     * @return
     */
    public static Calendar getAcademicYear(int year)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, Calendar.JULY);
        calendar.set(Calendar.DATE, 1);

        return calendar;
    }
}
